/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.adv.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkEnterpriseAdvReward;
import com.thinkgem.jeesite.modules.hk.adv.dao.HkEnterpriseAdvRewardDao;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkEnterpriseAdvGoods;
import com.thinkgem.jeesite.modules.hk.adv.dao.HkEnterpriseAdvGoodsDao;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkEnterpriseAdvIntegral;
import com.thinkgem.jeesite.modules.hk.adv.dao.HkEnterpriseAdvIntegralDao;

/**
 * 企业广告奖励配置Service
 * @author 罗天文
 * @version 2018-03-20
 */
@Service
@Transactional(readOnly = true)
public class HkEnterpriseAdvRewardService extends CrudService<HkEnterpriseAdvRewardDao, HkEnterpriseAdvReward> {

	@Autowired
	private HkEnterpriseAdvGoodsDao hkEnterpriseAdvGoodsDao;
	@Autowired
	private HkEnterpriseAdvIntegralDao hkEnterpriseAdvIntegralDao;
	
	public HkEnterpriseAdvReward get(String id) {
		HkEnterpriseAdvReward hkEnterpriseAdvReward = super.get(id);
		hkEnterpriseAdvReward.setHkEnterpriseAdvGoodsList(hkEnterpriseAdvGoodsDao.findList(new HkEnterpriseAdvGoods(hkEnterpriseAdvReward)));
		hkEnterpriseAdvReward.setHkEnterpriseAdvIntegralList(hkEnterpriseAdvIntegralDao.findList(new HkEnterpriseAdvIntegral(hkEnterpriseAdvReward)));
		return hkEnterpriseAdvReward;
	}
	
	public List<HkEnterpriseAdvReward> findList(HkEnterpriseAdvReward hkEnterpriseAdvReward) {
		return super.findList(hkEnterpriseAdvReward);
	}
	
	public Page<HkEnterpriseAdvReward> findPage(Page<HkEnterpriseAdvReward> page, HkEnterpriseAdvReward hkEnterpriseAdvReward) {
		return super.findPage(page, hkEnterpriseAdvReward);
	}
	
	@Transactional(readOnly = false)
	public void save(HkEnterpriseAdvReward hkEnterpriseAdvReward) {
		super.save(hkEnterpriseAdvReward);
		for (HkEnterpriseAdvGoods hkEnterpriseAdvGoods : hkEnterpriseAdvReward.getHkEnterpriseAdvGoodsList()){
			if (hkEnterpriseAdvGoods.getId() == null){
				continue;
			}
			if (HkEnterpriseAdvGoods.DEL_FLAG_NORMAL.equals(hkEnterpriseAdvGoods.getDelFlag())){
				hkEnterpriseAdvGoods.setCityId(hkEnterpriseAdvReward.getCity().getId());
				hkEnterpriseAdvGoods.setCountryId(hkEnterpriseAdvReward.getCountry().getId());
				hkEnterpriseAdvGoods.setProvinceId(hkEnterpriseAdvReward.getProvince().getId());
				hkEnterpriseAdvGoods.setEndDate(hkEnterpriseAdvReward.getEndDate());
				hkEnterpriseAdvGoods.setBeginDate(hkEnterpriseAdvReward.getBeginDate());
				hkEnterpriseAdvGoods.setEnterprise(hkEnterpriseAdvReward.getEnterprise());
				if (StringUtils.isBlank(hkEnterpriseAdvGoods.getId())){
					hkEnterpriseAdvGoods.setReward(hkEnterpriseAdvReward);
					hkEnterpriseAdvGoods.preInsert();
					hkEnterpriseAdvGoodsDao.insert(hkEnterpriseAdvGoods);
				}else{
					hkEnterpriseAdvGoods.preUpdate();
					hkEnterpriseAdvGoodsDao.update(hkEnterpriseAdvGoods);
				}
			}else{
				hkEnterpriseAdvGoodsDao.delete(hkEnterpriseAdvGoods);
			}
		}
		for (HkEnterpriseAdvIntegral hkEnterpriseAdvIntegral : hkEnterpriseAdvReward.getHkEnterpriseAdvIntegralList()){
			if (hkEnterpriseAdvIntegral.getId() == null){
				continue;
			}
			if (HkEnterpriseAdvIntegral.DEL_FLAG_NORMAL.equals(hkEnterpriseAdvIntegral.getDelFlag())){

				hkEnterpriseAdvIntegral.setCityId(hkEnterpriseAdvReward.getCity().getId());
				hkEnterpriseAdvIntegral.setCountryId(hkEnterpriseAdvReward.getCountry().getId());
				hkEnterpriseAdvIntegral.setProvinceId(hkEnterpriseAdvReward.getProvince().getId());
				hkEnterpriseAdvIntegral.setEndDate(hkEnterpriseAdvReward.getEndDate());
				hkEnterpriseAdvIntegral.setBeginDate(hkEnterpriseAdvReward.getBeginDate());
				hkEnterpriseAdvIntegral.setEnterprise(hkEnterpriseAdvReward.getEnterprise());

				if (StringUtils.isBlank(hkEnterpriseAdvIntegral.getId())){
					hkEnterpriseAdvIntegral.setReward(hkEnterpriseAdvReward);
					hkEnterpriseAdvIntegral.preInsert();
					hkEnterpriseAdvIntegralDao.insert(hkEnterpriseAdvIntegral);
				}else{
					hkEnterpriseAdvIntegral.preUpdate();
					hkEnterpriseAdvIntegralDao.update(hkEnterpriseAdvIntegral);
				}
			}else{
				hkEnterpriseAdvIntegralDao.delete(hkEnterpriseAdvIntegral);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(HkEnterpriseAdvReward hkEnterpriseAdvReward) {
		super.delete(hkEnterpriseAdvReward);
		hkEnterpriseAdvGoodsDao.delete(new HkEnterpriseAdvGoods(hkEnterpriseAdvReward));
		hkEnterpriseAdvIntegralDao.delete(new HkEnterpriseAdvIntegral(hkEnterpriseAdvReward));
	}
	
}